package cn.cyh.pet.service.impl;

import cn.cyh.basic.service.impl.BaseServiceImpl;
import cn.cyh.basic.uitls.CodeGenerateUtils;
import cn.cyh.basic.uitls.DistanceUtil;
import cn.cyh.basic.uitls.PageList;
import cn.cyh.basic.uitls.Point;
import cn.cyh.order.domain.PetAcquisitionOrder;
import cn.cyh.order.mapper.PetAcquisitionOrderMapper;
import cn.cyh.org.domain.Employee;
import cn.cyh.org.domain.Shop;
import cn.cyh.org.mapper.EmployeeMapper;
import cn.cyh.org.mapper.ShopMapper;
import cn.cyh.pet.domain.Pet;
import cn.cyh.pet.domain.SearchMasterMsg;
import cn.cyh.pet.mapper.SearchMasterMsgMapper;
import cn.cyh.pet.query.SearchMasterMsgQuery;
import cn.cyh.pet.service.IPetService;
import cn.cyh.pet.service.ISearchMasterMsgService;
import cn.cyh.user.domain.User;
import cn.cyh.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long loginInfoId) {
        // 当前登陆人
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        searchMasterMsg.setUser_id(user.getId());
        searchMasterMsg.setUser(user);
        // 获取距离最近的店铺
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        // 保存寻主消息
        searchMasterMsgMapper.save(searchMasterMsg);
    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        query.setState(0);// 查询待处理
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long id) {
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
    }
        query.setState(1);// 查询待处理
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> queryUserMsg(SearchMasterMsgQuery query, Long id) {

        // 当前登陆人
        User user = userMapper.loadByLoginInfoId(id);
        query.setUserId(user.getId());
        return super.queryPage(query);
    }

    @Override
    @Transactional
    public void handle(Pet pet, Long id) {
        // 修改消息状态
        searchMasterMsgMapper.updateStateHandle(pet.getSearch_master_msg_id());
        // 生成宠物信息+详情
        petService.save(pet);
        // 生成订单
        SearchMasterMsg masterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        PetAcquisitionOrder acquisitionOrder = pet2order(pet, masterMsg, employee.getId());
        petAcquisitionOrderMapper.save(acquisitionOrder);
        if (acquisitionOrder.getPrice().longValue()>0){
            System.out.println("支付金额是"+acquisitionOrder.getPrice());
        }
        // 支付订单
    }



    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
